/**
 * Copyright (c) Css Team
 * All rights reserved.
 *
 * This file DictMan.java creation date: [Jan 13, 2014 15:21:03 PM] by liuzhb
 * http://www.css.com.cn
 */
package com.css.apps.base.dict.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.dict.service.DictItem;
import com.css.apps.base.dict.service.DictMan;
import com.css.core.configuration.Environment;
import com.css.util.Ajax;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class DelDict extends AdminAction {
	
	private static Log log = LogFactory.getLog(DelDict.class);
	private String ids;

	public String adminGo() {
		if (StringHelper.isEmpty(ids)) {
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
			return Action.ERROR;
		}
		try {
			List<String> dictIdList = StringHelper.strToList(ids);
			// if(dictIdList.size()==1){
			// if(DictItem.getSDictByParentId(ids) !=null &&
			// DictItem.getSDictByParentId(ids).size()>0){
			// result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR,
			// Messages.getString("systemMsg.NoDeleteDict"));
			// return Action.ERROR;
			// }
			// DictMan.delDict(dictIdList);
			// }else{
//			int num = 0;
//			List<String> dictList = null;
			for (int i = 0; i < dictIdList.size(); i++) {
				String id = dictIdList.get(i);
//				dictList = StringHelper.strToList(id);
				List<String> childList = DictItem.getSDictByParentId(id);
				List<String> delchildList = new ArrayList<String>(); // 需要删除的关联子字典项
				delchildList.addAll(DictItem.getSDictByParentId(id));
				delchildList.add(id);
				while (childList != null && childList.size() > 0) { // 子字典项
					List<String> childs = new ArrayList<String>();
					childs.addAll(childList);
					for (String dictId : childList) {
						List<String> newchildList = DictItem.getSDictByParentId(dictId);
						if (newchildList != null && newchildList.size() != 0) { // 如果存在子字典项，继续向下查找，并且添加在需要删除的list中
							childs.addAll(newchildList);
							delchildList.addAll(newchildList);
						}
						childs.remove(dictId);
					}
					childList = childs;
				}
				// if(childList !=null && childList.size()>0){
				// num++;
				// }else{
				DictMan.delDict(delchildList);
				// }
			}
			// if(num==dictIdList.size()){
			// result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR,
			// Messages.getString("systemMsg.NoDeleteDict"));
			// return Action.ERROR;
			// }else if(num>=1 && num<dictIdList.size()){
			// String str_num=Integer.toString(num);
			// result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS,
			// Messages.getString("systemMsg.NoDeleteDictCount",new
			// String[]{str_num}));
			// return Action.SUCCESS;
			// }
			// }
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"));
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.dbFaild"));
			return Action.ERROR;
		}
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
}
